
$(function() {
	
	Tip.emp('业务员');
});

var Email = {

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.email = form.email.value;
				body.contact = form.contact.value;
				body.tel = form.tel.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('customer.php?do=Email.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_import: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=Email.audit&item=75', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"导入": function() {
						if (!confirm("确认是否导入？")) {
							return false;
						}
						
						var form = $("#form_add")[0];

						var body = { };
						body.emp = form.emp.value;
						body.data = form.data.value;

						$(this).dialog("close");

						$.post('customer.php?do=Email.importPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 批量更新
	 */
	dialog_uu: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=Email.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"更新": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};					
						body.field = form.field.value;
						body.data1 = form.data1.value;
						body.data2 = form.data2.value;

						$(this).dialog("close");

						var url = 'customer.php?do=Email.uuPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);
		
		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'customer.php?do=Email.edit&id=' + id;	
		var c2 = 'customer.php?do=Email.view&id=' + id;

		var c3 = 'contact.php?do=Customer.add&id=' + id;

		var c4 = 'customer.php?do=Enquiry.add&id=' + id;
		var c5 = 'customer.php?do=Enquiry.email&id=' + id;

		var a1 = ['编辑信息', 'url', 'Email.dialog_edit_', '600x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x400'];

		var a3 = ['升为客户', 'url', 'Email.dialog_customer', '650x420'];
		
		var a4 = ['发送邮件', 'url', 'Email.dialog_enquiry', '800x450'];
		var a5 = ['已发邮件', 'url', 'list'];
		
		if (status == 1) {
			var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c5;

			return popup(e, [a1, a2, a3, a4, a5], s);
		}
		
		var s = c2 + ", " + c5;

		return popup(e, [a2, a5], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_edit")[0];
				
				var body = {};
				body.id = form.id.value;
				body.email = form.email.value;
				body.contact = form.contact.value;
				body.tel = form.tel.value;
				body.content = form.content.value;
				body.status = form.status.value;

				$(this).dialog("close");

				var url = 'customer.php?do=Email.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_enquiry: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"发送": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.from = form.from.value;
				body.to = form.to.value;
				body.subject = form.subject.value;
				body.content = form.content.value;
				body.send = form.send.value;
				body.date = form.date.value + " " + form.time.value;

				$(this).dialog("close");
				
				var url = 'customer.php?do=Enquiry.addPost';

				$.post(url, body, function(data) {
					alert("发送成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},

	validate_add: function () {
		var validator = $("#form_add").validate({
			rules: {
				code: {
					remote: "contact.php?do=Customer.ajax_exist"
				},
				name: "required",
				fullname: "required",
				country: {
					required: true,
					category: true
				},
				address: "required"
			},
			messages: {
				code: {
					remote: jQuery.format("{0} 已经存在")
				},
				status: {
					required: "请选择一个状态"
				}
			},
			errorPlacement: function(label, element) {
				label.insertAfter(element);
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		for (var i = 0; i < z.length; i++) {
			// alert($.toJSON($("#contact" + z[i] + "").html()));
			var validator = $("#form" + z[i]).validate({
				rules: {
					linkman: "required",
					tel: {
						tel: true
					},
					fax: {
						fax: true
					},
					mobile: {
						mobile: true
					},
					email: {
						email: true
					}
				},
				errorPlacement: function(label, element) {
					label.insertAfter(element);
				},
				success: function(label) {
					label.html("&nbsp;").addClass("checked");
				}
			});

			if (!validator.form()) {
				return false;
			}
		}

		return true;
	},

	dialog_customer: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var linkman = $("input[name='linkman']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var im = $("input[name='im']");

				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					contact.linkman = $(linkman[i]).val();
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.im = $(im[i]).val();

					aa.push(contact);
				}

				var form = $("#form_add")[0];

				var body = { };
				body.id = form.id.value;
				body.code = form.code.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;

				body.contact = $.toJSON(aa);

				$(this).dialog("close");

				$.post('contact.php?do=Customer.email', body, function(data) {
					alert("操作成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	}	
};
